import { IPermissionResponsData, IPerResCommonData, IPermission } from "./type";
import request from "@/utils/request";

enum API {
  ALLPERMISSION_URL = "admin/acl/permission/query",
  MARYPERMISSION_URL = "admin/acl/permission/query",
  ADDMENU_URL = "/admin/acl/permission/create",
  UPDATEMENU_URL = "/admin/acl/permission/update",
  delmenu_URL = "/admin/acl/permission/delete",
}

export const getAllPermissionApi = () =>
  request.get<any, IPermissionResponsData>(API.ALLPERMISSION_URL);

export const getMaryPermissionApi = (ids: string[]) =>
  request.get<any, IPermissionResponsData>(
    API.MARYPERMISSION_URL + `?ids=${ids}`
  );

export const reqAddOrUpdateMenu = (data: IPermission) => {
  const { _id } = data;
  delete data.updatedAt;
  delete data.createdAt;
  if (_id) {
    //防止data数据更新时，mongoDB自动更新_id
    delete data._id;
    delete data.pid;
    return request.put<any, IPerResCommonData>(
      API.UPDATEMENU_URL + `/${_id}`,
      data
    );
  } else {
    return request.post<any, IPerResCommonData>(API.ADDMENU_URL, data);
  }
};

export const reqDeleteMenu = (id: string) =>
  request.delete<any, IPerResCommonData>(API.delmenu_URL + `/${id}`);
